
package com.netease.xone.itemview;

import protocol.XoneService;
import protocol.meta.User;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.xone.R;
import com.netease.xone.widget.LoadingImageView;

import db.manager.ManagerAccount;

public class RenderFansView {

    protected View userItemClick;
    protected TextView userName;
    protected TextView userSign;// 签名
    protected LinearLayout userFansButton;
    protected TextView userFansButtonText;
    protected LoadingImageView userAvatarImage;

    public RenderFansView(View v) {
        userItemClick = v;
        userName = (TextView)v.findViewById(R.id.user_name);
        userSign = (TextView)v.findViewById(R.id.user_sign);
        userFansButton = (LinearLayout)v.findViewById(R.id.user_fans_button);
        userFansButtonText = (TextView)v.findViewById(R.id.user_fans_button_text);
        userAvatarImage = (LoadingImageView)v.findViewById(R.id.user_avatar);
        // 初始化
        int dimension = (int)v.getResources().getDimension(R.dimen.common_avatar_size);
        userAvatarImage.setServerClipSize(dimension, dimension);
    }

    public void renderView(final Context context, final User user) {
        if(user.userId.equals(ManagerAccount.getInstance().getCurrentAccountId())){
            userFansButton.setVisibility(View.GONE);
        }else {
            userFansButton.setVisibility(View.VISIBLE);
        }
        float density = context.getResources().getDisplayMetrics().density;
        if (userItemClick != null) {
            userItemClick.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "test" + user.nickname, Toast.LENGTH_SHORT).show();
                }
            });
        }
        userAvatarImage.setImageResource(R.drawable.avatar_user_default_50);
        if (user.avatar != null) {
            userAvatarImage.setLoadingImage(user.avatar);
        }
        userName.setText(user.nickname);
        userSign.setText(user.signature);
        if (userFansButton != null) {
            userFansButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 关注或者取消关注
                    if (user.follow == 0) {
                        // 我未关注他，发起关注
                        XoneService.getInstance().doFollow(user.userId);
                    } else if (user.follow == 1) {
                        // 我已经关注他，发起取消关注
                        XoneService.getInstance().doCancelFollow(user.userId);
                    }

                }
            });
            if (user.follow == 0) {
                // 我未关注他
                // 以下这两种状态暂时相同，没有对方已关注我而我没关注对方这个状态
                userFansButton.setBackgroundResource(R.drawable.selector_blue_btn);
                userFansButtonText.setCompoundDrawablePadding((int)(5 * density));
                userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_add, 0,
                        0, 0);
                userFansButtonText.setText(R.string.add_follow);
                // if (user.followed == 0) {
                // // 他未关注我
                // } else if (user.followed == 1) {
                // // 他已经关注我
                // }
            } else if (user.follow == 1) {
                // 我已经关注他
                userFansButton.setBackgroundResource(R.drawable.selector_gray_btn);
                if (user.followed == 0) {
                    // 他未关注我
                    userFansButtonText.setCompoundDrawablePadding(0);
                    userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    userFansButtonText.setText(R.string.has_followed);
                } else if (user.followed == 1) {
                    // 他已经关注我
                    userFansButtonText.setCompoundDrawablePadding((int)(2 * density));
                    userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(
                            R.drawable.icon_exchange, 0, 0, 0);
                    userFansButtonText.setText(R.string.followed_eachother);
                }
            }
        }

    }
}
